# src/crud/sys_objects_field_rule.py
from typing import List, Optional
from sqlalchemy.orm import Session
from src.models import sys_objects_field_rule as models
from src.schemas import sys_objects_field_rule as schemas
from src.schemas import request_model
from src.core import orm_curd
from src.schemas.login_manager import AuthManager


# ---------------------- 单条新增 ----------------------
def create_field_rule(db: Session,
                      auth: AuthManager,
                      rule: schemas.SysObjectsFieldRuleCreate) -> dict:
    """新增单条字段规则"""
    return orm_curd.Insert(
        db,
        auth,
        models.SysObjectsFieldRule,
        schemas.SysObjectsFieldRule,
        "字段规则",
        [rule.dict()],
        ["api_id", "field_id", "rule_type", "rule_key"],  # 业务唯一键
        skip_duplicates=True
    )


# ---------------------- 批量新增 ----------------------
def create_field_rules(db: Session,
                       auth: AuthManager,
                       rules: List[schemas.SysObjectsFieldRuleCreate]) -> dict:
    """批量新增字段规则"""
    return orm_curd.Insert(
        db,
        auth,
        models.SysObjectsFieldRule,
        schemas.SysObjectsFieldRule,
        "字段规则",
        rules,
        ["api_id", "field_id", "rule_type", "rule_key"],
        skip_duplicates=True
    )


# ---------------------- 分页查询 ----------------------
def get_field_rules(db: Session,
                    auth: AuthManager,
                    page: int = 0,
                    page_size: int = 100,
                    FilterModel: Optional[request_model.QueryModel] = None,
                    id: Optional[str] = None) -> dict:
    """分页查询字段规则列表"""
    return orm_curd.query_with_page(
        db,
        auth,
        models.SysObjectsFieldRule,
        schemas.SysObjectsFieldRule,
        id=id,
        filter_model=FilterModel,
        page=page,
        page_size=page_size
    )


# ---------------------- 单条更新 ----------------------
def update_field_rule(db: Session,
                      auth: AuthManager,
                      rule_id: str,
                      rule: schemas.SysObjectsFieldRule) -> schemas.SysObjectsFieldRule:
    """更新单条字段规则"""
    update_model = request_model.UpdateModel([
        request_model.UpdateItem(
            pk_names=["id"],
            pk_values=[rule_id],
            data=rule.dict(exclude_unset=True)
        )
    ])
    return orm_curd.update(
        db,
        auth,
        models.SysObjectsFieldRule,
        update_model
    )


# ---------------------- 批量更新 ----------------------
def update_field_rules(db: Session,
                       auth: AuthManager,
                       updateModel: request_model.UpdateModel) -> dict:
    """批量更新字段规则"""
    return orm_curd.update(
        db,
        auth,
        models.SysObjectsFieldRule,
        updateModel
    )


# ---------------------- 删除 ----------------------
def delete_field_rules(db: Session,
                       auth: AuthManager,
                       deleteModel: request_model.DeleteModel) -> dict:
    """删除字段规则（硬删除）"""
    return orm_curd.delete(
        db=db,
        auth=auth,
        model_cls=models.SysObjectsFieldRule,
        delete_obj=deleteModel
    )
